#!/bin/bash

tip() {
    echo "请选择需要执行的操作："
    echo "   1 - 安装Server节点，需提供："
    echo "         a.私有镜像仓库的 IP 和 端口"
    echo "         b.私有镜像仓库的 账号 和 密码"
    echo "         c.数据库url，如：mysql://root:123456@tcp(192.168.181.126:3306)/k3s"
    echo "   2 - 卸载Server节点"
    echo "   3 - 安装Agent节点，需提供："
    echo "         a.K3S_TOKEN参数值 [在 Server 服务器上执行\"cat /var/lib/rancher/k3s/server/node-token\"获取]"
    echo "         b.Server的IP地址或域名"
    echo "         c.私有镜像仓库的 IP 和 端口"
    echo "         d.私有镜像仓库的 账号 和 密码"
    echo "   4 - 卸载Agent节点"
    echo "   5 - 退出"
    printf "请选择序号: "
}

install_server() {
    clear
    echo "--------------安装参数--------------"
    while true
    do
        printf "私有镜像仓库的 IP[C-退出]: "
        read image_ip
        if [ $image_ip = "C" -o $image_ip = "c" ] ; then
            exit 0
        fi
        if [ ! -z $image_ip ] ; then
            break
        fi
    done

    while true
    do
        printf "私有镜像仓库的 端口[C-退出]: "
        read image_port
        if [ $image_port = "C" -o $image_port = "c" ] ; then
            exit 0
        fi
        if [ ! -z $image_port ] ; then
            break
        fi
    done

    while true
    do
        printf "私有镜像仓库的 账号[C-退出]: "
        read image_user
        if [ $image_user = "C" -o $image_user = "c" ] ; then
            exit 0
        fi
        if [ ! -z $image_user ] ; then
            break
        fi
    done

    while true
    do
        printf "私有镜像仓库的 密码[C-退出]: "
        read image_pass
        if [ $image_pass = "C" -o $image_pass = "c" ] ; then
            exit 0
        fi
        if [ ! -z $image_pass ] ; then
            break
        fi
    done

    while true
    do
        printf "数据库 URL[C-退出]: "
        read db_url
        if [ $db_url = "C" -o $db_url = "c" ] ; then
            exit 0
        fi
        if [ ! -z $db_url ] ; then
            break
        fi
    done

    while true
    do
        clear
        echo "请确认如下信息是否正确："
        printf "\tIP:\t$image_ip\n"
        printf "\t端口:\t$image_port\n"
        printf "\t账号:\t$image_user\n"
        printf "\t密码:\t$image_pass\n"
        printf "\t数据库URL:\t$db_url\n"
        printf "继续(N)|退出(C):"
        read ifcontinue
        if [ $ifcontinue = "C" -o $ifcontinue = "c" ] ; then
            exit 0
        fi
        clear
        if [ $ifcontinue = "N" -o $ifcontinue = "n" ] ; then
            if [ "$1" = "on" ] ; then
                rpms=`sed -n '1p' ./image/list.txt`
                yum install -y ${rpms#*:}
                dn_url=`sed -n '2p' ./image/list.txt`
                wget -O ./image/k3s-airgap-images-amd64.tar ${dn_url#*:}
            else
                yum install -y ./image/*.rpm
            fi

            cd ./data
            ./server_install.sh $image_ip $image_port $image_user $image_pass $db_url 2>&1 &
            exit 0
        fi
    done
}

remove_server() {
    while true
    do
        clear
        printf "确认删除Server节点? 继续(C)|退出(N)："
        read ifcontinue
        if [ $ifcontinue = "C" -o $ifcontinue = "c" ] ; then
            exit 0
        fi
        if [ $ifcontinue = "N" -o $ifcontinue = "n" ] ; then
            cd ./data
            ./server_uninstall.sh
            exit 0
        fi
    done


}

while true
do
    clear
    tip
    read num
    case $num in
    1)
        install_server $1
        exit 0
    ;;
    2)
        remove_server
        exit 0
    ;;
    3)
        echo "3"
    ;;
    4)
        echo "4"
    ;;
    5)
        exit 0
    ;;
    esac
done

